exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    /**
    * 获取区域列表信息
    *
    * @returns
    */
    getAreaList() {
        var _this = this;

        return _asyncToGenerator(function* () {
            const db = _this.db;
            const attributes = ['id', 'area_code', 'area_name', 'parent_id'];
            // const where = {     [db.Sequelize.Op.or]: [{         parent_id: 0     }, {
            //   parent_id: 2     }] };
            try {
                return _this.CrudService.findAll('cms_area', {}, attributes);
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }
    /**
    * 获取区域列表信息（分页）
    *
    * @param {any} data
    * @returns
    */
    getAreaAllList(data) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            const db = _this2.db;
            data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
            const attributes = ['id', 'area_code', 'area_name', 'parent_id'];
            let where = {};
            if (data.area_name !== undefined || data.area_name !== null) {
                where = {
                    area_name: {
                        [_this2.db.Sequelize.Op.like]: '%' + data.area_name + '%'
                    }
                };
            }
            try {
                const res = yield db['cms_area'].findAndCountAll({
                    where: where,
                    order: [['id', 'DESC']],
                    limit: parseInt(data.pageSize),
                    offset: parseInt(data.start)
                });
                return res;
            } catch (err) {
                return _this2.exceptionService.handleError(err);
            }
        })();
    }

    /**
    * 获取区域所有信息
    *
    * @returns
    */
    getAreaData() {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            const db = _this3.db;
            const attributes = ['id', 'area_code', 'area_name', 'parent_id'];
            try {
                const res = yield db['cms_area'].findAll();
                return res;
            } catch (err) {
                console.log(err);
                return _this3.exceptionService.handleError(err);
            }
        })();
    }
    delArea(data) {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            const db = _this4.db;
            const aj = {
                success: false,
                msg: ''
            };
            try {
                const Res = yield db['cms_area'].findAll({
                    where: {
                        parent_id: data.id
                    }
                });
                if (Res.length > 0) {
                    aj.msg = '当前区域下存在子区域，请先删除子区域';
                    return aj;
                }
                const cellRes = yield db['cms_cell'].findAll({
                    where: {
                        area_id: data.id
                    }
                });
                if (cellRes.length > 0) {
                    aj.msg = '当前区域下存在单元格，请先删除单元格';
                    return aj;
                }
                const delRes = yield db['cms_area'].destroy({
                    where: {
                        id: data.id
                    }
                });
                aj.success = true;
                aj.msg = '删除成功';
                return aj;
            } catch (err) {
                return _this4.exceptionService.handleError(err);
            }
        })();
    }

    delAreaPath(data) {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            const db = _this5.db;
            const aj = {
                success: false,
                msg: ''
            };
            data.scope_path = null;
            try {
                const Res = yield db['cms_area'].update(data, {
                    where: {
                        id: data.id
                    }
                });
                aj.success = true;
                aj.msg = '删除成功';
                return aj;
            } catch (err) {
                return _this5.exceptionService.handleError(err);
            }
        })();
    }
    /**
    * 保存区域坐标信息
    *
    * @param {any} data
    * @returns
    */
    saveAreaScope(data) {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            const db = _this6.db;
            const aj = {
                success: false,
                msg: ''
            };
            data.scope_path = JSON.stringify(data.scope_path);
            console.log(data);
            const where = {};
            try {
                const Res = yield db['cms_area'].update(data, {
                    where: {
                        id: data.id
                    }
                });
                if (Res) {
                    aj.success = true;
                    aj.msg = '修改成功';
                } else {
                    aj.msg = '更新操作失败';
                }
                return aj;
            } catch (error) {
                return _this6.exceptionService.handleError(error);
            }
        })();
    }
    /**
    * 新增区域
    *
    * @param {any} data
    * @returns
    */
    saveNewArea(data) {
        var _this7 = this;

        return _asyncToGenerator(function* () {
            const db = _this7.db;
            const aj = {
                success: false,
                msg: ''
            };

            try {
                const oldRes = yield db['cms_area'].findAll({
                    where: {
                        area_code: data.area_code
                    }
                });
                if (oldRes.length > 0) {
                    aj.msg = '区域编码已存在，请重新输入';
                    return aj;
                } else {
                    const treeData = yield new _this7.TreeService().saveTree('cms_area', data.parent, data);
                    console.log('treeData', treeData);
                    aj.result = treeData;
                    aj.success = true;
                    aj.msg = '新增成功';

                    // aj.result=oldRes; const Res = await db['cms_area'].update(data, {     where:
                    // {         id: data.id     } }); if (Res) {     aj.success = true;     aj.msg
                    // = '修改成功'; } else {     aj.msg = '更新操作失败'; }
                    return aj;
                }
            } catch (error) {
                return _this7.exceptionService.handleError(error);
            }
        })();
    }

    quearCellByAreaId(data) {
        var _this8 = this;

        return _asyncToGenerator(function* () {
            const db = _this8.db;
            const aj = {
                success: false,
                msg: ''
            };
            try {
                const res = yield db['cms_cell'].count({
                    where: {
                        area_id: data.id
                    }
                });
                aj.success = true;
                aj.msg = '查询成功';
                aj.result = res;
                return aj;
            } catch (err) {
                console.log(err);
                return _this8.exceptionService.handleError(err);
            }
        })();
    }

    /**
    * 通过parentid查询区域信息
    *
    * @param {any} data
    * @returns
    */
    queryAreaByParentId(data) {
        var _this9 = this;

        return _asyncToGenerator(function* () {
            try {
                const db = _this9.db;
                const Res = yield db['cms_area'].findAll({
                    where: {
                        parent_id: data.parent_id
                    }
                });
                return Res;
            } catch (error) {
                return _this9.exceptionService.handleError(error);
            }
        })();
    }
};